<?php
namespace Admin\Controller;
class GoodsController extends CommonController {
		
	/*
	*
	* 商品列表
	*
	*/
	public function index(){
		
		$act=I('get.act');
       
        if (!empty($act)&&$act==='search') {
		   $keywords=trim(urldecode(I('keywords')));	
           $condition=array();
           $con=array();
           if(!empty($keywords)){
			 $condition['gname']=array('like','%'.$keywords.'%');
		    
           }
         $con['keywords']=$keywords;
        }
        
		
		$count=M('goods')->where($condition)->count();
		
        $p=getpage($count,9);
		 
		 foreach($con as $key=>$val) 
		 {
           $p->parameter[$key]  = urlencode($val);
         } 
		 
		 $goods_arr=M('goods')->where($condition)->order('sort asc ')->limit($p->firstRow.','.$p->listRows)->select();
		 
		 
		 foreach($goods_arr as $k=>$v){
			 
		 $goods_arr[$k]['gdesc']=msubstr(strip_tags($v['gdesc']),0,20,'utf-8',true);
			 
			 
		 }
		 
		 

		 $this->goods=$goods_arr;
		 
		 $this->assign('page',$p->show());
	 
         $this->assign('con',$con);
		 
		 $this->display();
 
	}
	
	/*
	*
	*  添加+修改
	*
	*
	*/
	public function add(){
		
		$sort = M('sort') -> select();
		$this -> assign('sort',$sort);
	    $act=I('act','');

		$id=I('id',0,'intval');

		if($act=='add')
		{
			$sort_id = I('post.sort_id');

			$gname=I('post.gname');

			$pics=I('post.pics');
			
			$gpoint=I('post.gpoint');
			
			$gdesc=$_POST['gdesc'];
		 
           !is_empty($gname,$pics,$gpoint)?$this->error(L('_IS_EMPTY_')):'';
   
           $sort=I('post.sort',0);

		    if(!empty($id)){

			$old_pics_arr=M('goods')->field('pics')->where(array('gid'=>$id))->find();

				if($old_pics_arr['pics']!==$pics)
				{
					!empty($old_pics_arr['pics'])?unlink($old_pics_arr['pics']):'';
					!empty($old_pics_arr['pics'])?unlink(split_original($old_pics_arr['pics'])):'';

					$pics=$this->base64_upload($pics,'goods_'); 	

				}else{

					$pics=$old_pics_arr['pics'];

				}

			}else{

				   $pics=$this->base64_upload($pics,'goods_'); 

			}

            $arr=array(
			  'gname'=>$gname,
			  
              'gpoint'=>$gpoint,
 
              'pics'=>$pics,
			  
			  'gdesc'=>$gdesc,
 
			  'sort'=>$sort,

			  'sort_id' => $sort_id
 
			); 
			
			 

			 $lastId=!empty($id)?M('goods')->where(array('gid'=>$id))->save($arr):M('goods')->add($arr);
			 
		 

			 if(!empty($lastId)){

				$this->success(L('_DO_SUCCEED_'),U('Goods/index')); 

				exit;

			 }

			 else{

               $this->error(L('_DO_FAILED_')); 				
			 }

		}

	    $this->edit=!empty($id)?M('goods')->where(array('gid'=>$id))->find():'';
		
		$get_sort_arr=M('goods')->field('sort')->order('sort desc')->limit(1)->find();
	 
		$this->get_sort=intval($get_sort_arr['sort'])+1;

		$this->display(); 
	}
	
	 /*
	 *
	 *  删除
	 */
	 	public function del(){
			
			
		$id=I('post.id',0,'intval');
		
		if(!empty($id)){
			
			$id_arr=is_array($id)?$id:array($id);
		   	
			foreach($id_arr as $k=>$v){
				 
				 $arr=M('goods')->field('pics,gdesc')->where(array('gid'=>$v))->find();
			
			    !empty($arr['pics'])?unlink($arr['pics']):'';//删除缩略图
				
				!empty($arr['pics'])?unlink(split_original($arr['pics'])):'';
				
				delete_img($arr['gdesc']);//删除文章中的图片
				  
				$delId=M('goods')->where(array('gid'=>$v))->delete();
				
				if(empty($delId)) continue;
	  
			}
			
			message(1,L('_DO_SUCCEED_'),U('Goods/index'));
		}		
		message(2,L('_DO_FAILED_'));
		
 
	}
	
    
 /*-----------------------------公共函数---------------------------------------*/ 
	  
	//百度富文本上传
	public function ueditor(){
		
	    $data = new \Org\Util\Ueditor();
	
        echo $data->output();
  	
	}
	  
	  

	 //上传图片

	public  function base64_upload($base64,$type='') {

			 

        $base64_image = str_replace(' ', '+', $base64);//post的数据里面，加号会被替换为空格，需要重新替换回来，如果不是post的数据，则注释掉这一行

       

    if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image, $result)){

		

        //匹配成功

			if($result[2] == 'jpeg'){

				

				$image_name = $type.date('YmdHis',time()).'.jpg'; //纯粹是看jpeg不爽才替换的

				

			}else{

				$image_name = $type.date('YmdHis',time()).'.'.$result[2];

			}

			

           $image_file = "./Uploads/{$image_name}";//服务器文件存储路径

        

			if (file_put_contents($image_file, base64_decode(str_replace($result[1], '', $base64_image)))){

				reSizeImg($image_file,400,300);

				return $image_file;

				

			}else{

				 

				return false;

			}

			

        }

		else

		{

			

           return false;

        }

     }

	
 
}

 